const { Controller,ActionHandler } = require('../lib/mvcEntity.js');
const mvcUtil = require('../lib/mvcUtil.js');

const BaseController = function(controllerName,actionWare){
    let baseActionWare = actionWare;
    let retController = new Controller(controllerName);
    retController.doAction = function(mappingUrl,requestUrl,request,response){
         var actionEntity = mvcUtil.parseActionEntity(mappingUrl,requestUrl);
         var actionStr = actionEntity.actionStr;
         actionStr = actionStr.substr(actionStr.lastIndexOf("/")+1);
         if(baseActionWare[actionStr]){
             baseActionWare[actionStr].doAction(request,response);
         }else{
             mvcUtil.writeJSONResponse(response,"404","not find "+requestUrl);
         }
    };
    return retController;
};

module.exports = BaseController;